package com.softeem.utils;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SqlSessionFactoryUtil {

	private static final String CONFIG = "mybatis-config.xml";
	private static SqlSessionFactory factory;
	
	static{
		InputStream is = null;
		try {
			is = Resources.getResourceAsStream(CONFIG);
			factory = new SqlSessionFactoryBuilder().build(is);
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				if(is != null){
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static SqlSession getSession(){
		return factory.openSession(true);
	}
	
	public static void main(String[] args) {
		System.out.println(getSession());
	}
}
